package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alan.modules.bean.*;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.handle.BaseExecption;
import net.alan.modules.service.IProjectService;
import net.alan.modules.service.INoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Api(tags = "通知管理")
@Controller
@RequestMapping(value = "notice")
public class NoticeController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IProjectService projectService;

    @ApiOperation(value = "确认已读通知", notes = "")
    @RequestMapping(value = "ok", method = RequestMethod.POST)
    @ResponseBody
    public Result ok(Integer id) throws Exception {
        Result result = noticeService.ok(id);
        return result;
    }

    @ApiOperation(value = "确认邀请", notes = "")
    @RequestMapping(value = "okproject", method = RequestMethod.POST)
    @ResponseBody
    public Result okproject(Integer id) throws Exception {
        Notice notice = noticeService.getById(Notice.class, id);
        try {
            projectService.joinProject(notice.getFromUserId(), notice.getRefId());
            notice.setReaded(true);
            notice.setRefId(null);
            this.noticeService.save(Notice.class,notice);
        } catch (BaseExecption baseExecption) {
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加系统通知", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(String content) throws Exception {
        Result result = noticeService.noticeAll(content);
        return result;
    }

    @ApiOperation(value = "删除", notes = "")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id) throws Exception {
        Result result = null;
        try {
            result = noticeService.delete(Favorite.class, id);
        } catch (BaseExecption baseExecption) {
            result = ResultUtil.error("删除异常");
        }
        return result;
    }


    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public PageModel list(PageModel pageModel) throws Exception {
        Page<Notice> list = this.noticeService.findByUserId(WebContextUtil.getUserId(), pageModel);
        return pageModel.toPageModel(list);
    }
}
